package sdfi.lee.DAO;

import java.sql.Connection;
import java.sql.Statement;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import sdfi.lee.Entity.Lab;
import sdfi.lee.Entity.Task;
import sdfi.lee.Entity.Yiqi;

public class LabDAO {
	public String labAdd(Lab lab)
	{
		Session session=null;
		Transaction ts=null;
		try
		{
			session=DAO.openSession();
			ts=session.beginTransaction();
			session.save(lab);
			ts.commit();
			return "success";
		}
		catch(Exception ee)
		{
			ts.rollback();
			System.out.println("Exception ee when you lab add");
			ee.printStackTrace();
			return "error";
		}
		finally
		{
			DAO.closeSession();
		}
	}
	
	public String taskAdd(Task task) throws Exception
	{
		Session session=null;
		Connection con=null;
		try
		{
			session=DAO.openSession();
			con=session.connection();
			String sql="insert into task(taskname,lid,taskdescribe)value('";
			sql+=task.getTaskname()+"','"+task.getLabid()+"','"+task.getTaskdescribe()+"')";
			Statement st=con.createStatement();
			st.execute(sql);
			con.commit();
			return "success";
			
		}
		catch(Exception ee)
		{
			con.rollback();
			ee.printStackTrace();
			return "error";
		}
		finally
		{
			DAO.closeSession();
		}
	}
	
	public String yiqiAdd(Yiqi yiqi) throws Exception
	{
		Session session=null;
		Connection con=null;
		try
		{
			session=DAO.openSession();
			con=session.connection();
			String sql="insert into yiqi(yiqiname,lid,yiqidescribe)value('";
			sql+=yiqi.getYiqiname()+"','"+yiqi.getLabid()+"','"+yiqi.getYiqidescribe()+"')";
			Statement st=con.createStatement();
			st.execute(sql);
			con.commit();
			return "success";
			
		}
		catch(Exception ee)
		{
			con.rollback();
			ee.printStackTrace();
			return "error";
		}
		finally
		{
			DAO.closeSession();
		}
	}
	public List<Lab> getAllLab(int page,int size)
	{
		List<Lab> list=null;
		Session session=null;
		Transaction ts=null;
		try
		{
			session=DAO.openSession();
			ts=session.beginTransaction();
			Criteria criteria=session.createCriteria(Lab.class);
			criteria.setFirstResult((page-1)*size);
			criteria.setMaxResults(size);
			list=criteria.list();
			ts.commit();
	
		}
		catch(Exception ee)
		{
			System.out.println("Exception ee when you get lab");
			ts.rollback();
			ee.printStackTrace();
		}
		finally
		{
			DAO.closeSession();
		}
		return list;
	}
	
	public Lab getLabById(Integer id)
	{
		Lab lab=null;
		Session session=null;
		Transaction ts=null;
		try
		{
			session=DAO.openSession();
			ts=session.beginTransaction();
			lab=(Lab)session.get(Lab.class, id);
			ts.commit();
			
		}
		catch(Exception ee)
		{
			System.out.println("Excption  call when you get lab");
			ts.rollback();
			ee.printStackTrace();
		}
		finally
		{
			DAO.closeSession();
		}
		return lab;
	}

	public  Task getTaskById(Integer id)
	{
		Task task=null;
		Session session=null;
		Transaction ts=null;
		try
		{
			session=DAO.openSession();
			ts=session.beginTransaction();
			task=(Task)session.get(Task.class, id);
			ts.commit();
			
		}
		catch(Exception ee)
		{
			System.out.println("Excption  call when you get task");
			ts.rollback();
			ee.printStackTrace();
		}
		finally
		{
			DAO.closeSession();
		}
		return task;
	}

	public  Yiqi getYiqiById(Integer id)
	{
		Yiqi yiqi=null;
		Session session=null;
		Transaction ts=null;
		try
		{
			session=DAO.openSession();
			ts=session.beginTransaction();
			yiqi=(Yiqi)session.get(Yiqi.class, id);
			ts.commit();
			
		}
		catch(Exception ee)
		{
			System.out.println("Excption  call when you get task");
			ts.rollback();
			ee.printStackTrace();
		}
		finally
		{
			DAO.closeSession();
		}
		return yiqi;
	}

	public List<Lab> getAllLab()
	{
		List<Lab> list=null;
		Session session=null;
		Transaction ts=null;
		try
		{
			session=DAO.openSession();
			ts=session.beginTransaction();
			Query query=session.createQuery("select new sdfi.lee.Entity.Lab(a.id,a.labname) from Lab a");
			list=query.list();
			
			ts.commit();
	
		}
		catch(Exception ee)
		{
			System.out.println("Exception ee when you get lab");
			ts.rollback();
			ee.printStackTrace();
		}
		finally
		{
			DAO.closeSession();
		}
		return list;
	}
}
